<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>

      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <BasicModal v-bind="$attrs" @register="registerModal" title="请输入条码" @ok="handleQr">
      <BasicForm @register="registerForm" />
    </BasicModal>
    <!-- <InputForm @register="registerDrawer" @success="handleSuccess" /> -->
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsCardKstLsProCardDataHListView',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { kstLsProCardDataHDelete, kstLsProCardDataHListData } from '/@/api/mes/card/kstLsProCardDataH';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './formLSView.vue';
  import { BasicModal, useModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';

  const { t } = useI18n('card.kstLsProCardDataH');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('拉手工艺参数管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('编号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('版本号'),
        field: 'verNum',
        component: 'Input',
      },
      {
        label: t('类型'),
        field: 'ctype',
        component: 'Select',
        componentProps: {
          dictType: 'mes_pro_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('编号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('版本号'),
      dataIndex: 'verNum',
      key: 'a.ver_num',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('类型'),
      dataIndex: 'ctype',
      key: 'a.ctype',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'mes_pro_type',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('修改时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        label: '查看',
        onClick: handleForm.bind(this, { id: record.id, code: record.code, verNum: record.verNum, ctype: record.ctype }),
        // auth: 'card:kstLsProCardDataH:edit',
      },
    ],
  };

  const [registerForm, { validate, setFieldsValue, getFieldsValue }] = useForm({
    labelWidth: 100,
    schemas: [
      {
        label: t('条码'),
        field: 'barCode',
        component: 'Input',
        required: true,
      },
      {
        label: t('id'),
        field: 'id',
        component: 'Input',
        required: true,
        show: false,
      },
      {
        label: t('版本号'),
        field: 'verNum',
        component: 'Input',
        required: true,
        show: false,
      },
      {
        label: t('类型'),
        field: 'ctype',
        component: 'Select',
        componentProps: { dictType: 'mes_pro_type' },
        required: true,
        show: false,
      },
      {
        label: t('条码'),
        field: 'code',
        component: 'Input',
        required: true,
        show: false,
      }
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerModal, { openModal, closeModal, setModalProps,setModalData }] = useModal();
  const [registerTable, { reload, getForm }] = useTable({
    api: kstLsProCardDataHListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    // openDrawer(true, record);
    // console.log(record);
    // window.open('/#/card/ls/' + record.code + '/' + record.verNum + '/' + record.ctype, '_blank');
    openModal(true, { id: record.id, code: record.code, verNum: record.verNum, ctype: record.ctype });
    nextTick(() => {
      setFieldsValue({ id: record.id, code: record.code, verNum: record.verNum, ctype: record.ctype });
    });
    // console.log(await getFieldsValue());
  }
  async function handleDelete(record: Recordable) {
    const res = await kstLsProCardDataHDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleQr(data: any, record: Recordable) {
    let dataValidate = await validate();
    // console.log('dataValidate==', dataValidate, data, record);
    window.open('/#/card/ls/' +  dataValidate.id + '/' + dataValidate.code + '/' + dataValidate.verNum + '/' + dataValidate.ctype + '/' + dataValidate.barCode, '_blank');
  }

  function handleSuccess() {
    reload();
  }
</script>
